<?php

// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;

class Area extends Common
{
    /**
     * User:Simony
     * Function: 地区表列表
     * @return mixed
     */
    public function areaList()
    {
        //params
        $field = input('get.field', '');
        $value = input('get.value', '');
        $search_field = input('get.search_field', '');
        $search_value = input('get.search_value', '');

        $pid = input('pid/d', 0);

        //where
        $where = [];

        //judge
        if ($field) $where[$field] = $value;
        if ($search_field) $where[$search_field] = $search_value;

        $where['pid'] = $pid;

        //model
        $areaModel = model('common/area/Area');

        //getData
        $list = $areaModel->getList($where, 0, '*', 'first_char asc');

        //assign
        $this->assign('list', $list);

        return $this->fetch();
    }


    /**
     * User:Simony
     * Function: 地区表添加修改
     */
    public function areaAdd()
    {


        //params
        $id = input('param.id/d', '');

        //model
        $areaModel = model('common/area/Area');
        $data = $areaModel->getInfoByID($id);

        if ($this->request->isAjax()) {

            //post params
            $ajax_data = $this->request->post();
            if (empty($ajax_data['name'])) ds_json_encode('err', '名称不能为空');
            $ajax_data['first_char'] = $ajax_data['first_char'] ?: get_first_char($ajax_data['name']);
            $ajax_data['dept'] = $ajax_data['pid'] > 0 ?$areaModel->getVal('dept', ['id' => $ajax_data['pid']]) + 1:1;

            //judge
            if ($id && !$data) ds_json_encode('err', '数据不存在');

            if ($id) {
                $childIds = $areaModel->getChildren($id);
                $childIds[] = $id;
                if (in_array($ajax_data['pid'], $childIds)) ds_json_encode('err', '当前数据上级不能是自己以及自己的子类');
                $res = $areaModel->edit(['id' => $id], $ajax_data);
            } else {
                $res = $areaModel->add($ajax_data);
            }

            $res ? ds_json_encode('ret', '操作成功') : ds_json_encode('err', '操作失败');
        }


        $list = $areaModel->getList([], 0, '*','first_char asc');
        $list = get_tree_option($list, 0, 'pid');

        $this->assign('data', $data);
        $this->assign('list', $list);
        return $this->fetch();
    }


    public function getArea()
    {
        if ($this->request->isAjax()) {
            $pid = input('pid/d', 0);
            $where['pid'] = $pid;
            //model
            $areaModel = model('common/area/Area');

            //getData
            $list = $areaModel->getList($where, 0, '*', 'first_char asc');

            return json_encode(['status' => 1, 'msg' => '获取成功', 'data' => $list]);
        }
    }

    /**
     * User:Simony
     * Function: 删除区域
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function areaDel()
    {
        if ($this->request->isAjax()) {

            $id = input('post.id/d', '');
            if (!$id) return ['err' => '缺少必要参数'];

            $areaModel = model('common/area/Area');
            $area = $areaModel->getInfoById($id);
            if (!$area) return ['err' => '数据不存在或已删除'];

            $areaList = $areaModel->getList(['pid' => $id]);

            if ($areaList) {
                return ['err' => '请先删除子地区后操作'];
            }

            if ($areaModel->delById($id)) {
                return ['ret' => '删除成功'];
            }
            return ['err' => '删除失败，请重试'];
        }
    }

}